package com.example.administrator.jingpingou.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.application.MyApplication;
import com.example.administrator.jingpingou.bean.MineCollectBean;
import com.example.administrator.jingpingou.bean.NextThemeBean;
import com.example.administrator.jingpingou.config.NetConfig;
import com.example.administrator.jingpingou.login.LoginActivity;

public class WebTaoActivity extends AppCompatActivity {

    private WebView mWebView;
    private String num_iid;
    private ImageView mImageBack,mImageCollect;
    private String dataBean;
    private NextThemeBean.DataBean dataBeanNew;
    private boolean flag=false;
    private boolean aTrue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_tao);
        Intent intent = getIntent();
        num_iid = intent.getStringExtra("num_iid");
        aTrue = intent.getBooleanExtra("true", true);
        dataBeanNew = (NextThemeBean.DataBean) intent.getSerializableExtra("dataBean");
        Log.d("num", "onCreate: num_iid=====numid:   "+num_iid);
        initView();
        initData();
        setListener();
    }

    private void setListener() {
        mImageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mImageCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(MyApplication.getUserName())){
                   startActivity(new Intent(WebTaoActivity.this,LoginActivity.class));
                }else {
                    String title = dataBeanNew.getTitle();
                    String now_price = dataBeanNew.getNow_price();
                    String pic_url = dataBeanNew.getPic_url();
                    String discount = dataBeanNew.getDiscount();
                    String origin_price = dataBeanNew.getOrigin_price();
                    MineCollectBean bean = new MineCollectBean();
                    bean.setNum_iid(num_iid);
                    bean.setDiscount(discount);
                    bean.setTitle(title);
                    bean.setPic_url(pic_url);
                    bean.setNow_price(now_price);
                    bean.setOrigin_price(origin_price);
                    bean.setUserName(MyApplication.getUserName());
                    bean.save();

                    Toast.makeText(WebTaoActivity.this, "收藏成功咯,去看看吧~~", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private void initData() {
        //设置WebView属性，能够执行Javascript脚本
        mWebView.getSettings().setJavaScriptEnabled(true);
        //加载需要显示的网页
        String path = String.format(NetConfig.PATH_TAO_WEB,num_iid);
        Log.d("tag", "initData: ====pathWebTao   "+path);
        mWebView.loadUrl(path);
        //设置Web视图
//        setContentView(mWebView);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebChromeClient(new WebChromeClient());
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);

    }

    private void initView() {
        mWebView = (WebView) findViewById(R.id.wv_activity_web_tao);
        mImageBack = (ImageView) findViewById(R.id.iv_web_tao_back);
        mImageCollect = (ImageView) findViewById(R.id.iv_activity_web_tao);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack(); //goBack()表示返回WebView的上一页面
            return true;
        }
        return false;

    }

//    private class MyWebViewClient extends WebViewClient {
//        @Override
//        public boolean shouldOverrideUrlLoading(WebView view, String url) {
//            view.loadUrl(url);
//            return true;
//        }
//    }
}
